#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;
ll a[N], w[N], prefix[N];

void solve() {
	int n; cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) cin >> w[i];
	ll ess = 0;
	for (int i = 1; i <= n; i++) ess += a[i] * w[i];
	ll mi = 0, fix = 0;
	for (int i = 1; i <= n; i++) {
		prefix[i] = prefix[i - 1] + (a[i] ? -1 : 1) * w[i];
		fix = max(fix, prefix[i] - mi);
		mi = min(mi, prefix[i]);
	}
	cout << ess + fix << '\n';
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}